package com.laiyima.zhongjiang.linghuilv.demo.Serviceprovider;

import android.content.Context;
import android.content.SharedPreferences;
import android.graphics.Color;
import android.os.Build;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.WindowManager;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.Toast;

import com.laiyima.zhongjiang.linghuilv.demo.R;
import com.laiyima.zhongjiang.linghuilv.demo.adapter.FreezeAdapter;
import com.laiyima.zhongjiang.linghuilv.demo.bean.FreezeBean;

import org.json.JSONArray;
import org.json.JSONObject;
import org.xutils.common.Callback;
import org.xutils.http.RequestParams;
import org.xutils.x;

import java.util.ArrayList;
import java.util.List;

import me.imid.swipebacklayout.lib.app.SwipeBackActivity;

public class FreezeInfoActivity extends SwipeBackActivity {
    private LinearLayout linearLayout,no_FreezeInfo;
    private ListView listView;
    private int freeCode;
    private String TAG="FreezeInfoActivity";
    private FreezeBean mFreezeBean;
    private FreezeAdapter mFreezeAdapter;
    private List<FreezeBean> ListBean = new ArrayList<>();

    protected boolean useThemestatusBarColor = false;
    protected void setStatusBar() {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {//5.0及以上
            View decorView = getWindow().getDecorView();
            int option = View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
                    | View.SYSTEM_UI_FLAG_LAYOUT_STABLE;
            decorView.setSystemUiVisibility(option);
            //根据上面设置是否对状态栏单独设置颜色
            if (useThemestatusBarColor) {
                // getWindow().setStatusBarColor(getResources().getColor(R.color.colorTheme));
            } else {
                getWindow().setStatusBarColor(Color.TRANSPARENT);
            }
        } else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {//4.4到5.0
            WindowManager.LayoutParams localLayoutParams = getWindow().getAttributes();
            localLayoutParams.flags = (WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS | localLayoutParams.flags);
        }
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {//android6.0以后可以对状态栏文字颜色和图标进行修改
            getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN | View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR);
        }
    }
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_freeze_info);
        setStatusBar();
        linearLayout= (LinearLayout) findViewById(R.id.freeze_back);
        linearLayout.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        no_FreezeInfo= (LinearLayout) findViewById(R.id.no_FreezeInfo);
        listView= (ListView) findViewById(R.id.freeze_list);
        intView();

    }

    private void intView() {
        SharedPreferences sharedPreferences = getSharedPreferences("userinfo.txt", Context.MODE_PRIVATE);
        String userID = sharedPreferences.getString("userID", "");
        RequestParams params = new RequestParams("http://zljl.laiima.com/index.php?s=/lfl_api/Commonlfl/freeze");
        //需要带入的参数
        params.addBodyParameter("uid", userID);
         x.http().post(params, new Callback.CommonCallback<String>() {
            @Override
            public void onSuccess(String result) {
                try {
                    JSONObject jsonObject = new JSONObject(result);
                    freeCode = jsonObject.optInt("code", 0);
                    JSONArray list = jsonObject.getJSONArray("data");
                    for (int i = 0; i < list.length(); i++) {
                        JSONObject value = list.getJSONObject(i);
                        String sn = value.getString("sn");
                        String money = value.getString("money");
                        String deal = value.getString("deal");
                        mFreezeBean = new FreezeBean(sn,money,deal);
                        ListBean.add(mFreezeBean);
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }

                Log.e(TAG, "回调成功" + result);
                if (freeCode == 200) {
                    mFreezeAdapter = new FreezeAdapter(getBaseContext().getApplicationContext(),ListBean);
                    mFreezeAdapter.notifyDataSetChanged();
                    listView.setAdapter(mFreezeAdapter);

                }
                if (freeCode == 201) {


                }
                if (freeCode == 500) {
                    Toast.makeText(getBaseContext().getApplicationContext(), "系统异常", Toast.LENGTH_SHORT).show();
                    Log.e(TAG, "系统异常");
                }else{


                }
            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {
                Log.e(TAG, "回调失败");
            }
            @Override
            public void onCancelled(CancelledException cex) {

            }
            @Override
            public void onFinished() {
            if(ListBean.size()==0){
                no_FreezeInfo.setVisibility(View.VISIBLE);
            }else {
                no_FreezeInfo.setVisibility(View.GONE);
            }
            }
        });
    }
}
